package com.ruoyi.flink.util;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @Description: ResultSet转实体类
 * @ClassName: GetBeanFromResultUtil
 * @author syc
 * @date: 2022年5月26日 下午7:51:46
 */
public class GetBeanFromResultUtil {

	private GetBeanFromResultUtil() {
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static <T> List getBeans(ResultSet resultSet, Class className) {
		List list = new ArrayList();
		Field fields[] = className.getDeclaredFields();
		try {
			while (resultSet.next()) {
				T instance = (T) className.newInstance();
				for (Field field : fields) {
					Object result = resultSet.getObject(field.getName());
					boolean flag = field.isAccessible();
					field.setAccessible(true);
					field.set(instance, result);
					field.setAccessible(flag);
				}
				list.add(instance);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return list;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static <T> T getBean(ResultSet resultSet, Class className) {
		T instance = null;
		try {
			instance = (T) className.newInstance();
			Field fields[] = className.getDeclaredFields();
			for (Field field : fields) {
				Object result = resultSet.getObject(field.getName());
				boolean flag = field.isAccessible();
				field.setAccessible(true);
				field.set(instance, result);
				field.setAccessible(flag);
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return instance;
	}

	/**
	 * 将resultSet转化为JSONObject
	 * 
	 * @param rs
	 * @return
	 * @throws SQLException
	 * @throws JSONException
	 */

	public static JSONObject resultSetToJsonObject(ResultSet rs) throws SQLException {

		// json对象

		JSONObject jsonObj = new JSONObject();

		// 获取列数

		ResultSetMetaData metaData = rs.getMetaData();

		int columnCount = metaData.getColumnCount();

		// 遍历ResultSet中的每条数据

		if (rs.next()) {

			// 遍历每一列

			for (int i = 1; i <= columnCount; i++) {

				String columnName = metaData.getColumnLabel(i);

				String value = rs.getString(columnName);

				jsonObj.put(columnName, value);

			}

		}

		return jsonObj;

	}

	/**
	 * 将resultSet转化为JSON数组
	 * 
	 * @param rs
	 * @return
	 * @throws SQLException
	 * @throws JSONException
	 */

	public static JSONArray resultSetToJsonArry(ResultSet rs) throws SQLException{
		// json数组
		JSONArray array = new JSONArray();
		// 获取列数

		ResultSetMetaData metaData = rs.getMetaData();

		int columnCount = metaData.getColumnCount();

		JSONObject jsonObj;
		// 遍历ResultSet中的每条数据

		while (rs.next()) {
			jsonObj = new JSONObject();
			// 遍历每一列
			for (int i = 1; i <= columnCount; i++) {
				String columnName = metaData.getColumnLabel(i);
				String value = rs.getString(columnName);
				jsonObj.put(columnName, value);
			}

			array.add(jsonObj);

		}

		return array;

	}

	
//	4.JSONObject--->JAVABean
//	User user=JSONObject.toBean(jsonObj,User.class);
//
//	5.JSON字符串--->JSONObject
//	JSONObject jsonObj=JSONObject.fromObject(jsonStr);
//
//	6.JAVABean--->JSONObject
//	JSONObject jsonObj=JSONObject.fromObject(javabean);
//
//	7.强大JSON工具网址
//	http://www.bejson.com/json2javapojo/

	
	
	
}
